{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Masking Personal Information"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: maskPII"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #隐藏个人信息"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一条个人信息字符串 <code>s</code> ，可能表示一个 <strong>邮箱地址</strong> ，也可能表示一串 <strong>电话号码</strong> 。返回按如下规则 <strong>隐藏</strong> 个人信息后的结果：</p>\n",
    "\n",
    "<p><em><strong>电子邮件地址：</strong></em></p>\n",
    "\n",
    "<p>一个电子邮件地址由以下部分组成：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>一个 <strong>名字</strong> ，由大小写英文字母组成，后面跟着</li>\n",
    "\t<li>一个 <code>'@'</code> 字符，后面跟着</li>\n",
    "\t<li>一个 <strong>域名</strong> ，由大小写英文字母和一个位于中间的 <code>'.'</code> 字符组成。<code>'.'</code> 不会是域名的第一个或者最后一个字符。</li>\n",
    "</ul>\n",
    "\n",
    "<p>要想隐藏电子邮件地址中的个人信息：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><strong>名字</strong> 和 <strong>域名</strong> 部分的大写英文字母应当转换成小写英文字母。</li>\n",
    "\t<li><strong>名字</strong> 中间的字母（即，除第一个和最后一个字母外）必须用 5 个 <code>\"*****\"</code> 替换。</li>\n",
    "</ul>\n",
    "\n",
    "<p><em><strong>电话号码：</strong></em></p>\n",
    "\n",
    "<p>一个电话号码应当按下述格式组成：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>电话号码可以由 10-13 位数字组成</li>\n",
    "\t<li>后 10 位构成 <strong>本地号码</strong></li>\n",
    "\t<li>前面剩下的 0-3 位，构成 <strong>国家代码</strong></li>\n",
    "\t<li>利用 <code>{'+', '-', '(', ')', ' '}</code> 这些 <strong>分隔字符</strong> 按某种形式对上述数字进行分隔</li>\n",
    "</ul>\n",
    "\n",
    "<p>要想隐藏电话号码中的个人信息：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>移除所有 <strong>分隔字符</strong></li>\n",
    "\t<li>隐藏个人信息后的电话号码应该遵从这种格式：\n",
    "\t<ul>\n",
    "\t\t<li><code>\"***-***-XXXX\"</code> 如果国家代码为 0 位数字</li>\n",
    "\t\t<li><code>\"+*-***-***-XXXX\"</code> 如果国家代码为 1 位数字</li>\n",
    "\t\t<li><code>\"+**-***-***-XXXX\"</code> 如果国家代码为 2 位数字</li>\n",
    "\t\t<li><code>\"+***-***-***-XXXX\"</code> 如果国家代码为 3 位数字</li>\n",
    "\t</ul>\n",
    "\t</li>\n",
    "\t<li><code>\"XXXX\"</code> 是最后 4 位 <strong>本地号码</strong></li>\n",
    "</ul>\n",
    "&nbsp;\n",
    "\n",
    "<div class=\"top-view__1vxA\">\n",
    "<div class=\"original__bRMd\">\n",
    "<div>\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"LeetCode@LeetCode.com\"\n",
    "<strong>输出：</strong>\"l*****e@leetcode.com\"\n",
    "<strong>解释：</strong>s 是一个电子邮件地址。\n",
    "名字和域名都转换为小写，名字的中间用 5 个 * 替换。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"AB@qq.com\"\n",
    "<strong>输出：</strong>\"a*****b@qq.com\"\n",
    "<strong>解释：</strong>s 是一个电子邮件地址。\n",
    "名字和域名都转换为小写，名字的中间用 5 个 * 替换。\n",
    "注意，尽管 \"ab\" 只有两个字符，但中间仍然必须有 5 个 * 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"1(234)567-890\"\n",
    "<strong>输出：</strong>\"***-***-7890\"\n",
    "<strong>解释：</strong>s 是一个电话号码。\n",
    "共计 10 位数字，所以本地号码为 10 位数字，国家代码为 0 位数字。\n",
    "因此，隐藏后的电话号码应该是 \"***-***-7890\" 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>s</code> 是一个 <strong>有效</strong> 的电子邮件或者电话号码</li>\n",
    "\t<li>如果 <code>s</code> 是一个电子邮件：\n",
    "\t<ul>\n",
    "\t\t<li><code>8 &lt;= s.length &lt;= 40</code></li>\n",
    "\t\t<li><code>s</code> 是由大小写英文字母，恰好一个 <code>'@'</code> 字符，以及 <code>'.'</code> 字符组成</li>\n",
    "\t</ul>\n",
    "\t</li>\n",
    "\t<li>如果 <code>s</code> 是一个电话号码：\n",
    "\t<ul>\n",
    "\t\t<li><code>10 &lt;= s.length &lt;= 20</code></li>\n",
    "\t\t<li><code>s</code> 是由数字、空格、字符 <code>'('</code>、<code>')'</code>、<code>'-'</code> 和 <code>'+'</code> 组成</li>\n",
    "\t</ul>\n",
    "\t</li>\n",
    "</ul>\n",
    "</div>\n",
    "</div>\n",
    "</div>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [masking-personal-information](https://leetcode.cn/problems/masking-personal-information/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [masking-personal-information](https://leetcode.cn/problems/masking-personal-information/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"LeetCode@LeetCode.com\"', '\"AB@qq.com\"', '\"1(234)567-890\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if s[0].isalpha():\n",
    "            s = s.lower()\n",
    "            return s[0] + '*****' + s[s.find('@') - 1:]\n",
    "        s = ''.join(c for c in s if c.isdigit())\n",
    "        cnt = len(s) - 10\n",
    "        suf = '***-***-' + s[-4:]\n",
    "        return suf if cnt == 0 else f'+{\"*\" * cnt}-{suf}'\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if '@' in s:\n",
    "            s = s.lower()\n",
    "            i = s.index('@')\n",
    "            return s[0] + '*****' + s[i-1] + s[i:]\n",
    "        else:\n",
    "            s = s.replace(\"-\", \"\")\n",
    "            s = s.replace('+', \"\")\n",
    "            s = s.replace('(', \"\")\n",
    "            s = s.replace(')', \"\")\n",
    "            s = s.replace(' ', \"\")\n",
    "            if len(s) > 10:\n",
    "                return '+' + '*' * len(s[:-10]) + '-***-***-' + s[-4:]\n",
    "            else:\n",
    "                return '***-***-' + s[-4:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        n=len(s)\n",
    "        ans=''\n",
    "        if '@' in s:    #电子邮件\n",
    "            s=s.lower()\n",
    "            ans+=s[0]+'*'*5\n",
    "            i=0\n",
    "            while i<n and s[i]!='@':\n",
    "                i+=1\n",
    "            ans+=s[i-1]+'@'+s[i+1:]\n",
    "        else:\n",
    "            cnt=sum(x in '0123456789' for x in s)\n",
    "            c=cnt-10 \n",
    "            if c==0:\n",
    "                ans+='***-***-'\n",
    "            elif c==1:\n",
    "                ans+='+*-***-***-'\n",
    "            elif c==2:\n",
    "                ans+='+**-***-***-'\n",
    "            else:\n",
    "                ans+='+***-***-***-'\n",
    "            t=''\n",
    "            for x in s[::-1]:\n",
    "                if x in '0123456789':\n",
    "                    t+=x\n",
    "                if len(t)==4:\n",
    "                    break\n",
    "            ans+=t[::-1]\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        at = s.find(\"@\")\n",
    "        if at >= 0:\n",
    "            return (s[0] + \"*****\" + s[at - 1:]).lower()\n",
    "        s = \"\".join(i for i in s if i.isdigit())\n",
    "        return [\"\", \"+*-\", \"+**-\", \"+***-\"][len(s) - 10] + \"***-***-\" + s[-4:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        at = s.find('@')\n",
    "        if at >= 0:\n",
    "            return (s[0] + '*' * 5 + s[at-1:]).lower()\n",
    "        s = \"\".join(i for i in s if i.isdigit())\n",
    "        return [\"\", \"+*-\", \"+**-\", \"+***-\"][len(s)-10] + \"***-***-\" + s[-4:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        at = s.find('@')\n",
    "        if at >= 0:\n",
    "            return (s[0] + '*' * 5 + s[at - 1:]).lower()\n",
    "        s = \"\".join(i for i in s if i.isdigit())\n",
    "        cnt = len(s)\n",
    "        if cnt == 10:\n",
    "            s = \"***-***-\" + s[-4:]\n",
    "        else:\n",
    "            s = \"+\" + ('*' * (cnt - 10)) + '-***-***-' + s[-4:]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        at=s.find('@')\n",
    "        if at>=0:\n",
    "            return (s[0]+\"*\"*5+s[at-1:]).lower()\n",
    "        s=\"\".join(i for i in s if i.isdigit())\n",
    "        return [\"\",\"+*-\",\"+**-\",\"+***-\"][len(s)-10]+\"***-***-\"+s[-4:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def maskPII(self, s: str) -> str:\r\n",
    "        encode = ''\r\n",
    "        if s.find('@') == -1:\r\n",
    "            s = ''.join(filter(str.isdigit, s))\r\n",
    "            local = '***-***-' + s[-4:]\r\n",
    "            pre = ''\r\n",
    "            if (x := len(s) - 10) > 0:\r\n",
    "                pre = '+' + '*'*x + '-'\r\n",
    "            encode = pre + local\r\n",
    "        else:\r\n",
    "            s = s.lower()\r\n",
    "            x = s.split('@')\r\n",
    "            x[0] = x[0][0] + '*'*5 + x[0][-1]\r\n",
    "            encode = '@'.join(x)\r\n",
    "        return encode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        # print(help(str()))\n",
    "        if '@' in s:\n",
    "            eIndex = s.index('@')\n",
    "            name = s[:eIndex].lower()\n",
    "            email = s[eIndex+1:].lower()\n",
    "            return f\"{name[0]}*****{name[-1]}@{email}\"\n",
    "        else:\n",
    "            for c in ['+', '-', '(', ')', ' ']:\n",
    "                s = s.replace(c, '')\n",
    "            ns = len(s)\n",
    "            preS = ''\n",
    "            if ns > 10:\n",
    "                preS = '+' + '*' * (ns - 10) + '-'\n",
    "            return f\"{preS}***-***-{s[-4:]}\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        p=[str(i) for i in range(10)]\n",
    "        def check(x):\n",
    "            if ord(x)<96:\n",
    "                x=chr(ord(x)+32)\n",
    "            return x\n",
    "        def seer(s):\n",
    "            pr=''\n",
    "            for i in range(len(s)):\n",
    "                if s[i]=='.':\n",
    "                    pr+='.'\n",
    "                    continue\n",
    "                pr+=check(s[i])\n",
    "            return pr\n",
    "        if '@' in s:\n",
    "            x=s.index('@')\n",
    "\n",
    "            a=check(s[0])\n",
    "            b=check(s[x-1])\n",
    "            ans=a+'*'*5+b+'@'+seer(s[x+1::])\n",
    "        else:\n",
    "            tot=0\n",
    "            ss=''\n",
    "            for i in s:\n",
    "                if i in p:\n",
    "                    tot+=1\n",
    "                    ss+=i\n",
    "            x=ss[tot-4:tot]\n",
    "            if tot==10:\n",
    "                ans='***-***-'+x\n",
    "            elif tot==11:\n",
    "                ans='+*-***-***-'+x\n",
    "            elif tot==12:\n",
    "                ans='+**-***-***-'+x\n",
    "            else:\n",
    "                ans='+***-***-***-'+x\n",
    "        return ans\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if not s[0].isalpha():\n",
    "            ds = []\n",
    "            for l in s:\n",
    "                if l.isdigit(): ds.append(l)\n",
    "            tmp = str(''.join(ds[len(ds) - 4: ]))\n",
    "            if len(ds) == 10: return '***-***-' + tmp\n",
    "            if len(ds) == 11: return '+*-***-***-' + tmp\n",
    "            if len(ds) == 12: return '+**-***-***-' + tmp\n",
    "            return '+***-***-***-' + tmp\n",
    "        part1, part2 = s.split('@')\n",
    "        return part1[0].lower() + '*****' + part1[-1].lower() + '@' + part2.lower()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def processPhone(self, s):\n",
    "        new_s = ''\n",
    "        for item in s:\n",
    "            if item.isdigit():\n",
    "                new_s += item\n",
    "\n",
    "        if len(new_s) < 10:\n",
    "            return 0\n",
    "        elif len(new_s) > 13:\n",
    "            return 0\n",
    "        \n",
    "        # Last four digits\n",
    "        last4 = new_s[-4:]\n",
    "\n",
    "        # national number\n",
    "        national_number = len(new_s) - 10\n",
    "\n",
    "        if national_number == 0:\n",
    "            return '***-***-' + ''.join(last4)\n",
    "        else:\n",
    "            return '+' + national_number * '*' + '-***-***-' + ''.join(last4)\n",
    "\n",
    "    def processEmail(self, s):\n",
    "        temp_list = s.split('@')\n",
    "        fst_chr = temp_list[0][0].lower()\n",
    "        lst_chr = temp_list[0][-1].lower()\n",
    "        name_space = fst_chr + '*****' + lst_chr + '@'\n",
    "        return name_space + temp_list[1].lower()\n",
    "\n",
    "\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if '@' in s:\n",
    "            # This maybe an email address\n",
    "            return self.processEmail(s)\n",
    "        else:\n",
    "            result = self.processPhone(s)\n",
    "            if result != 0:\n",
    "                return result\n",
    "            # This maybe an phone number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if '@' in s:\n",
    "            t = s.lower().split('@')\n",
    "            return t[0][0] + '*****' + t[0][-1] + '@' + t[1]\n",
    "        else:\n",
    "            s = ''.join([c for c in s if c.isdigit()])\n",
    "            return [\"\", \"+*-\", \"+**-\", \"+***-\"][len(s) - 10] + \"***-***-\" + s[-4:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        lst=[]\n",
    "        lst1=[]\n",
    "        lst2=[]\n",
    "        for i in range(len(s)):\n",
    "            lst1.append(s[i])\n",
    "        n=len(lst1)\n",
    "        \n",
    "        k=0\n",
    "        if s[0].isupper() or s[0].islower():\n",
    "            for j in range(len(lst1)):\n",
    "                if s[j]=='@':\n",
    "                    k=j\n",
    "                    break\n",
    "            lst.append(s[0].lower())\n",
    "            lst.append('*****')\n",
    "            lst.append(s[k-1].lower())    \n",
    "            for j in range(k,n):    \n",
    "                if s[j].isupper():\n",
    "                    lst.append(s[j].lower())\n",
    "                else:\n",
    "                    lst.append(s[j])\n",
    "        else:   \n",
    "            for u in range(n):\n",
    "                if lst1[u]!='+' and lst1[u]!='-' and lst1[u]!='(' and lst1[u]!=')' and lst1[u]!=' ':\n",
    "                    lst2.append(lst1[u])\n",
    "            g=len(lst2)\n",
    "            if g==10:\n",
    "                lst.append('***-***-')\n",
    "                for i in range(6,10):\n",
    "                    lst.append(lst2[i])\n",
    "            elif g==11:\n",
    "                lst.append('+*-***-***-')\n",
    "                for i in range(7,11):\n",
    "                    lst.append(lst2[i])\n",
    "            elif g==12:\n",
    "                lst.append('+**-***-***-')\n",
    "                for i in range(8,12):\n",
    "                    lst.append(lst2[i])\n",
    "            else:\n",
    "                lst.append('+***-***-***-')\n",
    "                for i in range(9,13):\n",
    "                    lst.append(lst2[i])\n",
    "        return \"\".join(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "\n",
    "        if '@' in s:\n",
    "            #email\n",
    "            name, domain = s.split('@')\n",
    "            name = name.lower()\n",
    "            domain = domain.lower()\n",
    "\n",
    "            return f'{name[0]}*****{name[-1]}@{domain}'\n",
    "        else:\n",
    "            #phone\n",
    "\n",
    "            s = s.replace('(', \"\").replace(')', '').replace('-', '').replace('+', '').replace(' ', '')\n",
    "\n",
    "            pre = s[:-10]\n",
    "            post = s[-10:]\n",
    "\n",
    "            country_part = ''\n",
    "            if pre:\n",
    "                country_part = f'+{\"*\"*len(pre)}-'\n",
    "            \n",
    "            return f'{country_part}***-***-{post[-4:]}'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        email_index = s.find('@')\n",
    "        s_list = list()\n",
    "        if email_index != -1:\n",
    "            return (s[0] + '*****' + s[email_index - 1:]).lower()\n",
    "        else:\n",
    "            for letter in s:\n",
    "                if letter.isdigit():\n",
    "                    s_list.append(letter)\n",
    "            if len(s_list) == 10:\n",
    "                return \"***-***-\" + ''.join(s_list[6:])\n",
    "            elif len(s_list) == 11:\n",
    "                return \"+*-***-***-\" + ''.join(s_list[7:])\n",
    "            elif len(s_list) == 12:\n",
    "                return \"+**-***-***-\" + ''.join(s_list[8:])\n",
    "            else:\n",
    "                return \"+***-***-***-\" + ''.join(s_list[9:])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if '@' in s:\n",
    "            s = s.lower()\n",
    "            l, r = s.split('@')\n",
    "            return l[0] + '*****' + l[-1] + '@' + r\n",
    "        else:\n",
    "            nums = []\n",
    "            for c in s:\n",
    "                if c.isdigit():\n",
    "                    nums.append(c)\n",
    "            ends = ''.join(nums[-4:])\n",
    "            if len(nums) == 10:\n",
    "                return '***-***-' + ends\n",
    "            elif len(nums) == 11:\n",
    "                return '+*-***-***-' + ends\n",
    "            elif len(nums) == 12:\n",
    "                return '+**-***-***-' + ends\n",
    "            else:\n",
    "                return '+***-***-***-' + ends"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        def email(str1):\n",
    "            li = str1.split('@')\n",
    "            li[0] = li[0].lower()\n",
    "            li[1] = li[1].lower()\n",
    "            li[0] = li[0][0]+'*****'+li[0][len(li[0])-1]\n",
    "            return li[0]+'@'+li[1]\n",
    "        def phone(str1):\n",
    "            for i in ['(',')','-','+',' ']:\n",
    "                str1 = str1.replace(i,'')\n",
    "            print(str1)\n",
    "            if len(str1)>10:\n",
    "                national_code = str1[0:len(str1)-10]\n",
    "                print(national_code)\n",
    "                phone_code = str1[len(str1)-10:len(str1)]\n",
    "                print(phone_code)\n",
    "                return '+'+'*'*len(national_code) + '-***-***-' + phone_code[6:]\n",
    "            else:\n",
    "                return '***-***-'+str1[6:]\n",
    "        if '@' in s:\n",
    "            return email(s)\n",
    "        else:\n",
    "            return phone(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if '@' in s:\n",
    "            name, web = s.split('@')\n",
    "            name2 = name[0].lower() + \"*****\" + name[-1].lower()\n",
    "            web2 = web.lower()\n",
    "            return name2+'@'+web2\n",
    "        else:\n",
    "            for i in ['+', '-', '(', ')', ' ']:\n",
    "                s = s.replace(i, '')\n",
    "            l = len(s)\n",
    "            countryn = l -10\n",
    "            res = '+'*(countryn>0) + countryn*'*' + '-'*(countryn>0)\n",
    "            res += '***-***-' + s[-4:]\n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if \"@\" in s:\n",
    "            return self.mask_email(s)\n",
    "        else:\n",
    "            return self.mask_phone(s)\n",
    "\n",
    "    def mask_email(self, s):\n",
    "        s = s.lower()\n",
    "        name, domain = s.split(\"@\")\n",
    "        return name[0] + \"*****\" + name[-1] + \"@\" + domain\n",
    "\n",
    "    def mask_phone(self, s):\n",
    "        digits = [c for c in s if c.isdigit()]\n",
    "        local = \"***-***-\" + \"\".join(digits[-4:])\n",
    "        if len(digits) == 10:\n",
    "            return local\n",
    "        else:\n",
    "            return \"+\" + \"*\" * (len(digits) - 10) + \"-\" + local\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if \"@\" in s:\n",
    "            # email\n",
    "            return s.split(\"@\")[0][0].lower() +\"*\"*5 + s.split(\"@\")[0][-1].lower() + \"@\" + s.split(\"@\")[1].lower()\n",
    "\n",
    "        else:\n",
    "            #  电话号码\n",
    "            telNo = s.replace(\"+\", \"\").replace(\"(\", \"\").replace(\")\",\"\").replace(\" \",\"\").replace(\"-\", \"\")\n",
    "            if len(telNo) == 10:\n",
    "                return \"***-***-\"+telNo[-4:]\n",
    "            elif len(telNo) == 11:\n",
    "                return \"+*-***-***-\"+telNo[-4:]\n",
    "            elif len(telNo) == 12:\n",
    "                return \"+**-***-***-\"+telNo[-4:]\n",
    "            elif len(telNo) == 13:\n",
    "                return \"+***-***-***-\"+telNo[-4:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        lst=[]\n",
    "        lst1=[]\n",
    "        lst2=[]\n",
    "        for i in range(len(s)):\n",
    "            lst1.append(s[i])\n",
    "        n=len(lst1)\n",
    "        \n",
    "        k=0\n",
    "        if s[0].isupper() or s[0].islower():\n",
    "            for j in range(len(lst1)):\n",
    "                if s[j]=='@':\n",
    "                    k=j\n",
    "                    break\n",
    "            lst.append(s[0].lower())\n",
    "            lst.append('*****')\n",
    "            lst.append(s[k-1].lower())    \n",
    "            for j in range(k,n):    \n",
    "                if s[j].isupper():\n",
    "                    lst.append(s[j].lower())\n",
    "                else:\n",
    "                    lst.append(s[j])\n",
    "        else:   \n",
    "            for u in range(n):\n",
    "                if lst1[u]!='+' and lst1[u]!='-' and lst1[u]!='(' and lst1[u]!=')' and lst1[u]!=' ':\n",
    "                    lst2.append(lst1[u])\n",
    "            g=len(lst2)\n",
    "            if g==10:\n",
    "                lst.append('***-***-')\n",
    "                for i in range(6,10):\n",
    "                    lst.append(lst2[i])\n",
    "            elif g==11:\n",
    "                lst.append('+*-***-***-')\n",
    "                for i in range(7,11):\n",
    "                    lst.append(lst2[i])\n",
    "            elif g==12:\n",
    "                lst.append('+**-***-***-')\n",
    "                for i in range(8,12):\n",
    "                    lst.append(lst2[i])\n",
    "            else:\n",
    "                lst.append('+***-***-***-')\n",
    "                for i in range(9,13):\n",
    "                    lst.append(lst2[i])\n",
    "        return \"\".join(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if '@' in s:\n",
    "            name, web = s.split('@')\n",
    "            #name2 = name[0].lower() + \"*****\" + name[-1].lower()\n",
    "            #web2 = web.lower()\n",
    "            return (name[0].lower() + \"*****\" + name[-1].lower()+'@'+web.lower())\n",
    "        else:\n",
    "            for i in ['+', '-', '(', ')', ' ']:\n",
    "                s = s.replace(i, '')\n",
    "            l = len(s)\n",
    "            countryn = l -10\n",
    "            res = '+'*(countryn>0) + countryn*'*' + '-'*(countryn>0) + '***-***-' + s[-4:]\n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        res = \"\"\n",
    "        if s.count(\"@\") > 0:\n",
    "            _index = s.index(\"@\")\n",
    "            s = s.lower()\n",
    "            res = f\"{s[0]}*****{s[_index - 1]}@{s[_index + 1:]}\"\n",
    "        else:\n",
    "            arr = []\n",
    "            for c in s:\n",
    "                if '0' <= c <= '9':\n",
    "                    arr.append(c)\n",
    "            if len(arr) == 10:\n",
    "                res = f\"***-***-{''.join(arr[-4:])}\"\n",
    "            else:\n",
    "                res = f\"+{'*' * (len(arr) - 10)}-***-***-{''.join(arr[-4:])}\"\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if s[0].isalpha():  # 电子邮件\n",
    "            return (s[0] + '*****' + s[s.index('@')-1:]).lower()\n",
    "\n",
    "        else:   # 电话号码\n",
    "            s = [c for c in s if c.isdigit()]   # 去除分隔符\n",
    "            end = '***-***-' + ''.join(s[-4:])\n",
    "            if len(s) == 10:\n",
    "                return end\n",
    "            return '+' + ('*' * (len(s) - 10)) + '-' + end\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        #电子邮件\n",
    "        if('@' in s):\n",
    "            name, domain = s.split('@')\n",
    "            name, domain = str.lower(name), str.lower(domain)\n",
    "            return name[0]+'*****'+name[-1]+'@'+domain\n",
    "        else:\n",
    "            telenumber = ''\n",
    "            for c in s:\n",
    "                if(48 <= ord(c) <= 57):\n",
    "                    telenumber += c\n",
    "            number = telenumber[-10:]\n",
    "            code = len(telenumber[:len(telenumber)-10])\n",
    "            if(code == 0):\n",
    "                return \"***-***-\" + number[-4:]\n",
    "            elif(code == 1):\n",
    "                return \"+*-***-***-\" + number[-4:]\n",
    "            elif(code == 2):\n",
    "                return \"+**-***-***-\" + number[-4:]\n",
    "            else:\n",
    "                return \"+***-***-***-\" + number[-4:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    @staticmethod\n",
    "    def check_email(s):\n",
    "        email = False\n",
    "        empty = ''\n",
    "        if s.find('@') < 0:\n",
    "            return email, empty, empty\n",
    "        pre, post = s.split('@')\n",
    "        if post.find('.') < 0 or post.startswith('.') or post.endswith('.'):\n",
    "            return email, empty, empty\n",
    "        post_0, post_1 = post.split('.')\n",
    "        if not Solution.check_char(post_0) or not Solution.check_char(post_1):\n",
    "            return email, empty, empty\n",
    "        if not Solution.check_char(pre):\n",
    "            return email, empty, empty\n",
    "        return True, pre.lower(), post.lower()\n",
    "\n",
    "\n",
    "    @staticmethod\n",
    "    def check_char(char):\n",
    "        if not char:\n",
    "            return False\n",
    "        for alphabet in char:\n",
    "            if alphabet.isspace():\n",
    "                return False\n",
    "            if not alphabet.isalpha():\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "\n",
    "    @staticmethod\n",
    "    def hide_email(pre, post):\n",
    "        if len(pre) < 2:\n",
    "            return pre + '@' + post\n",
    "        return pre[0] + '*****' + pre[-1] + '@' + post\n",
    "\n",
    "\n",
    "    @staticmethod\n",
    "    def check_tel(s):\n",
    "        tel = False\n",
    "        empty = ''\n",
    "        s_replace = s.replace('(', '').replace(')', '').replace('-', '').replace('+', '').replace(' ', '')\n",
    "        len_s = len(s_replace)\n",
    "        if len_s == 10:\n",
    "            return True, empty, s_replace[-4:]\n",
    "        elif len_s == 11:\n",
    "            return True, '*-***-***-', s_replace[-4:]\n",
    "        elif len_s == 12:\n",
    "            return True, '**-***-***-', s_replace[-4:]\n",
    "        elif len_s == 13:\n",
    "            return True, '***-***-***-', s_replace[-4:]\n",
    "        else:\n",
    "            return tel, empty, empty\n",
    "\n",
    "    \n",
    "    @staticmethod\n",
    "    def hide_tel(pre, post):\n",
    "        if not pre:\n",
    "            return '***-***-' + post\n",
    "        return '+' + pre + post\n",
    "\n",
    "\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        is_email, pre_email, post_email = Solution.check_email(s)\n",
    "        if is_email:\n",
    "            return Solution.hide_email(pre_email, post_email)\n",
    "\n",
    "        is_tel, pre_tel, post_tel = Solution.check_tel(s)\n",
    "        if is_tel:\n",
    "            return Solution.hide_tel(pre_tel, post_tel)\n",
    "\n",
    "        return s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if \"@\" in s:\n",
    "            return self.maskEmail(s)\n",
    "        else:\n",
    "            return self.maskPhone(s)\n",
    "\n",
    "    def maskEmail(self, s: str) -> str:\n",
    "        s = s.lower()\n",
    "        name, domain = s.split(\"@\")\n",
    "        name = name[0] + \"*****\" + name[-1]\n",
    "        return f\"{name}@{domain}\"\n",
    "\n",
    "    def maskPhone(self, s: str) -> str:\n",
    "        s = (\n",
    "            s.replace(\"(\", \"\")\n",
    "            .replace(\")\", \"\")\n",
    "            .replace(\"-\", \"\")\n",
    "            .replace(\"+\", \"\")\n",
    "            .replace(\" \", \"\")\n",
    "        )\n",
    "        if len(s) == 10:\n",
    "            return f\"***-***-{s[6:]}\"\n",
    "        elif len(s) == 11:\n",
    "            return f\"+*-***-***-{s[7:]}\"\n",
    "        elif len(s) == 12:\n",
    "            return f\"+**-***-***-{s[8:]}\"\n",
    "        else:\n",
    "            return f\"+***-***-***-{s[9:]}\"\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    pass\n",
    "    # assert \"l*****e@leetcode.com\" == Solution().maskPII(\"LeetCode@LeetCode.com\")\n",
    "    # assert \"a*****b@qq.com\" == Solution().maskPII(\"AB@qq.com\")\n",
    "    # assert \"***-***-7890\" == Solution().maskPII(\"1(234)567-890\")\n",
    "    # assert \"+***-***-***-3711\" == Solution().maskPII(\"(3906)2 07143 711\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if \"@\" in s:\n",
    "            t = s.split(\"@\")\n",
    "            t[1] = t[1].lower()\n",
    "            if len(t[0]) == 1:\n",
    "                return \"\".join(t[0].lower() + [\"@\"] + t[1])\n",
    "            else:\n",
    "                t[0] = t[0][0].lower() + \"*\"*5 + t[0][-1].lower() + \"@\"\n",
    "                return \"\".join(t)\n",
    "        else:\n",
    "            s = re.sub(r\"[()+-]\", \"\", s)\n",
    "            s = s.replace(\" \", \"\")\n",
    "            n = len(s)\n",
    "            if n == 10:\n",
    "                return \"***-***-\"+s[-4:]\n",
    "            if n == 11:\n",
    "                return \"+*-***-***-\"+s[-4:]\n",
    "            if n == 12:\n",
    "                return \"+**-***-***-\" + s[-4:]\n",
    "            if n == 13:\n",
    "                return \"+***-***-***-\" + s[-4:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if '@' in s:\n",
    "            s = s.lower()\n",
    "            index = s.find('@')\n",
    "            s = s[0] + '*****' + s[index-1:]\n",
    "        else:\n",
    "            s = s.replace('(', '').replace(')', '').replace('+', '').replace('-', '').replace(' ', '')\n",
    "            match len(s):\n",
    "                case 10:\n",
    "                    s = '***-***-' + s[-4:]\n",
    "                case 11:\n",
    "                    s = '+*-***-***-' + s[-4:]\n",
    "                case 12:\n",
    "                    s = '+**-***-***-' + s[-4:]\n",
    "                case 13:\n",
    "                    s = '+***-***-***-' + s[-4:]\n",
    "\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        # 判断 s 是否为邮箱地址\n",
    "        if \"@\" in s:\n",
    "            first, after = s.lower().split(\"@\")\n",
    "            return f\"{first[0]}*****{first[-1]}@{after}\"\n",
    "        else:\n",
    "            # 移除所有非数字字符\n",
    "            digits = \"\".join(filter(str.isdigit, s))\n",
    "            local = \"***-***-\" + digits[-4:]\n",
    "            # 判断并添加国家代码\n",
    "            if len(digits) == 10:\n",
    "                return local\n",
    "            else:\n",
    "                return \"+\" + \"*\" * (len(digits) - 10) + \"-\" + local\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if s[0].isalpha():\n",
    "            s = s.lower()\n",
    "            return s[0] + '*****' + s[s.find('@') - 1:]\n",
    "        s = ''.join(c for c in s if c.isdigit())\n",
    "        cnt = len(s) - 10\n",
    "        suf = '***-***-' + s[-4:]\n",
    "        return suf if cnt == 0 else f'+{\"*\" * cnt}-{suf}'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def maskPII(self, s: str) -> str:\r\n",
    "        if s[0].isalpha():\r\n",
    "            s = s.lower()\r\n",
    "            return s[0] + '*****' + s[s.find('@') - 1:]\r\n",
    "        else:\r\n",
    "            s = ''.join(c for c in s if c.isdigit())\r\n",
    "            cnt = len(s) - 10\r\n",
    "            local = '***-***-' + s[-4:]\r\n",
    "            return local if cnt == 0 else f\"+{'*' * cnt}-{local}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        final = \"\"\n",
    "        if s.find(\"@\") > -1:\n",
    "            list_s = s.split(\"@\")\n",
    "            if len(list_s[0]) < 2:\n",
    "                final += list_s[0].lower()\n",
    "            else:\n",
    "                temp = list_s[0].lower()\n",
    "                final += temp[0] + \"*****\" + temp[-1]\n",
    "            final += '@'\n",
    "            final += list_s[1].lower()\n",
    "        else:\n",
    "            temp = \"\"\n",
    "            for item in s:\n",
    "                if item.isdigit():\n",
    "                    temp += item\n",
    "            n = len(temp)\n",
    "            if n == 10:\n",
    "                final = \"***-***-\" + temp[6:]\n",
    "            else:\n",
    "                final = '+' + \"*\"*(n-10) + '-***-***-' + temp[(n-4):]\n",
    "        return final\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        email_index = s.find('@')\n",
    "        s_list = list()\n",
    "        if email_index != -1:\n",
    "            s_list.append(s[0].lower())\n",
    "            s_list.append('*' * 5)\n",
    "            s_list.append(s[email_index - 1].lower())\n",
    "            return ''.join(s_list) + s[email_index:].lower()\n",
    "        else:\n",
    "            for letter in s:\n",
    "                if letter.isdigit():\n",
    "                    s_list.append(letter)\n",
    "            if len(s_list) == 10:\n",
    "                return \"***-***-\" + ''.join(s_list[6:])\n",
    "            elif len(s_list) == 11:\n",
    "                return \"+*-***-***-\" + ''.join(s_list[7:])\n",
    "            elif len(s_list) == 12:\n",
    "                return \"+**-***-***-\" + ''.join(s_list[8:])\n",
    "            else:\n",
    "                return \"+***-***-***-\" + ''.join(s_list[9:])\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if '@' in s:\n",
    "            # 电子邮箱\n",
    "            s = s.lower()\n",
    "            pos = s.index('@')\n",
    "            s = s[0] + '*****' + s[pos - 1:]\n",
    "        else:\n",
    "            # 电话号码\n",
    "            # 去掉所有分割字符\n",
    "            for x in ['+', '-', '(', ')', ' ']:\n",
    "                s = s.replace(x, '')\n",
    "            # 判断长度\n",
    "            n = len(s)\n",
    "            if n == 10:\n",
    "                # 无国家码\n",
    "                s = '***-***-' + s[-4:]\n",
    "            else:\n",
    "                # 有国家码\n",
    "                s = '+' + ('*' * (n - 10)) + '-***-***-' + s[-4:]\n",
    "        return s\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if \"@\" in s:\n",
    "            mail_lower = s.lower()\n",
    "            mail_split_list = mail_lower.split(\"@\")\n",
    "            \n",
    "            mail_encoded = mail_split_list[0][0]+\"*****\"+mail_split_list[0][-1]+\"@\"+mail_split_list[1]\n",
    "            \n",
    "            return mail_encoded\n",
    "        \n",
    "        else:\n",
    "            #去除分隔符号\n",
    "            s_pure = \"\"\n",
    "            for num in s:\n",
    "                if num not in ('+', '-', '(', ')', ' '):     \n",
    "                    s_pure += num\n",
    "                \n",
    "            if len(s_pure)==10:\n",
    "                return \"***-***-\"+s_pure[-4:]\n",
    "\n",
    "            if len(s_pure)==11:\n",
    "                return \"+*-***-***-\"+s_pure[-4:]\n",
    "            if len(s_pure)==12:\n",
    "                return \"+**-***-***-\"+s_pure[-4:]\n",
    "            if len(s_pure)==13:\n",
    "                 return \"+***-***-***-\"+s_pure[-4:]\n",
    "\n",
    "             \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        at_pos = s.find(\"@\")\n",
    "        if at_pos >= 0:\n",
    "            s = s.lower()\n",
    "            s = s[:1] + \"*****\" + s[at_pos-1:]\n",
    "        else:\n",
    "            s = s.replace(\"+\", \"\")\n",
    "            s = s.replace(\"-\", \"\")\n",
    "            s = s.replace(\"(\", \"\")\n",
    "            s = s.replace(\")\", \"\")\n",
    "            s = s.replace(\" \", \"\")\n",
    "            if len(s)==10:\n",
    "                s = \"***-***-\"+s[-4:]\n",
    "            elif len(s)==11:\n",
    "                s = \"+*-***-***-\"+s[-4:]\n",
    "            elif len(s) == 12:\n",
    "                s = \"+**-***-***-\"+s[-4:]\n",
    "            else:\n",
    "                s=\"+***-***-***-\"+s[-4:]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "\n",
    "\n",
    "        try:\n",
    "            s_index = s.index(\"@\")\n",
    "            s=s.lower()\n",
    "            s=s[0]+\"*****\"+s[s_index-1:]\n",
    "            print(s)\n",
    "            return s\n",
    "\n",
    "        \n",
    "\n",
    "        except:\n",
    "            s=s.replace(\"+\",\"\")\n",
    "            s=s.replace(\"-\",\"\")\n",
    "            s=s.replace(\"(\",\"\")\n",
    "            s=s.replace(\")\",\"\")\n",
    "            s=s.replace(\" \",\"\")\n",
    "            print(s)\n",
    "            if len(s)==10:\n",
    "                return \"***-***-\"+s[-4:]\n",
    "            if len(s)==11:\n",
    "                return \"+*-***-***-\"+s[-4:]\n",
    "            if len(s)==12:\n",
    "                return \"+**-***-***-\"+s[-4:]\n",
    "            if len(s)==13:\n",
    "                return \"+***-***-***-\"+s[-4:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if s[0].isalpha():  # 电子邮件\n",
    "            s = s.lower()\n",
    "            name, domain = s.split('@')\n",
    "            return name[0] + '*****' + name[-1] + '@' + domain\n",
    "\n",
    "        else:   # 电话号码\n",
    "            s = [c for c in s if c.isdigit()]   # 去除分隔符\n",
    "            end = ''.join(s[-4:])\n",
    "            if len(s) == 10:\n",
    "                return '***-***-' + end\n",
    "            return '+' + ('*' * (len(s) - 10)) + '-***-***-' + end\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def maskPII(self, s: str) -> str:\r\n",
    "        phone_chars=set('+-({')\r\n",
    "        if s[0] in phone_chars or s[0].isdigit():\r\n",
    "            nums=[]\r\n",
    "            for c in s:\r\n",
    "                if c.isdigit():\r\n",
    "                    nums.append(c)\r\n",
    "            if len(nums)>10:\r\n",
    "                return '+'+'*'*(len(nums)-10)+'-***-***-'+''.join(nums[-4:])\r\n",
    "            else:\r\n",
    "                return '***-***-'+''.join(nums[-4:])\r\n",
    "        \r\n",
    "        idx=0\r\n",
    "\r\n",
    "        while s[idx]!='@':\r\n",
    "            idx+=1\r\n",
    "        name=s[0].lower()+'*****'+s[idx-1].lower()\r\n",
    "        return name+s[idx:].lower()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        map=set()\n",
    "        for i in range(10):\n",
    "            map.add(str(i))\n",
    "        print(map)\n",
    "        if \"@\" in s:\n",
    "            s=s.lower()\n",
    "            print(s)\n",
    "            name=\"\"\n",
    "            domain=\"\"\n",
    "            flag=0\n",
    "            for i in s:\n",
    "                if i!=\"@\" and flag!=1:\n",
    "                    name+=i\n",
    "                else:\n",
    "                    domain+=i\n",
    "                    flag=1\n",
    "            return name[0]+\"*****\"+name[-1]+domain\n",
    "        tel=\"\"\n",
    "        for i in s:\n",
    "            if i in map:\n",
    "                tel+=i\n",
    "        print(tel)\n",
    "        return \"+\"*(0 if len(tel)-10==0 else 1)+\"*\"*(len(tel)-10)+\"-\"*(0 if len(tel)-10==0 else 1)+\"***-***-\"+tel[len(tel)-4:]\n",
    "\n",
    "        \n",
    "                    \n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def maskPII(self, s: str) -> str:\r\n",
    "        phone_chars=set('+-({')\r\n",
    "        if s[0] in phone_chars or s[0].isdigit():\r\n",
    "            nums=[]\r\n",
    "            for c in s:\r\n",
    "                if c.isdigit():\r\n",
    "                    nums.append(c)\r\n",
    "            if len(nums)>10:\r\n",
    "                return '+'+'*'*(len(nums)-10)+'-***-***-'+''.join(nums[len(nums)-4:len(nums)])\r\n",
    "            else:\r\n",
    "                return '***-***-'+''.join(nums[6:10])\r\n",
    "        \r\n",
    "        idx=0\r\n",
    "        L=len(s)\r\n",
    "        while s[idx]!='@':\r\n",
    "            idx+=1\r\n",
    "        name=s[0].lower()+'*****'+s[idx-1].lower()\r\n",
    "        _idx=idx\r\n",
    "        while s[idx]!='.':\r\n",
    "            idx+=1\r\n",
    "        domain=s[_idx:idx].lower()\r\n",
    "        return name+domain+s[idx:].lower()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        if \"@\" in s:\n",
    "            head = s.split(\"@\")[0]\n",
    "            head = head[0] + \"*****\" + head[-1]\n",
    "            return head.lower() + \"@\" + s.split(\"@\")[1].lower()\n",
    "        else :\n",
    "            s = s.replace(\"-\",\"\").replace(\"+\",\"\").replace(\"(\",\"\").replace(\")\",\"\").replace(\" \",\"\")\n",
    "            ret = \"\"\n",
    "            if len(s) > 10:\n",
    "                ret = \"+\"\n",
    "                ret = ret + \"*\" * (len(s) - 10) \n",
    "                ret += \"-\"\n",
    "                s= s[len(s) - 10:]\n",
    "            ret = ret + \"***-***-\"\n",
    "            ret = ret + s[-4:]\n",
    "            return ret\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def maskPII(self, s: str) -> str:\n",
    "        dic={'(':0,')':0,'-':0 , '+':0}\n",
    "        if \"@\" in s:\n",
    "            head,tail=s.split('@')\n",
    "            name=head[0].lower()+\"*****\"+head[-1].lower()\n",
    "            A,B=tail.split(\".\")\n",
    "            A=A.lower()\n",
    "            ans=name+\"@\"+A+\".\"+B.lower()\n",
    "            return ans\n",
    "        else:\n",
    "            num=re.sub(r'\\D','',s)\n",
    "            tmp={10:\"***-***-XXXX\",\n",
    "            11:\"+*-***-***-XXXX\",\n",
    "            12:\"+**-***-***-XXXX\",\n",
    "            13:\"+***-***-***-XXXX\"}\n",
    "            ans=tmp[len(num)].replace(\"XXXX\",num[-4:])\n",
    "            return ans\n",
    "s=Solution()\n",
    "print(s.maskPII(\"1(234)567-890\"))\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
